<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class User extends Common
{
	
	function index()
	{
		$data = array();
    	$where = array();
    	$order='id desc';
		$State = input('get.State');
		$Smallchange = input('get.Smallchange');
		$AddTimeSort = input('get.AddTimeSort');
		$LandTimeSort = input('get.LandTimeSort');
        $StarAddTime= input('get.StarAddTime');
        $EndAddTime = input('get.EndAddTime');
		$StarLandTime= input('get.StarLandTime');
        $EndLandTime = input('get.EndLandTime');
    	$Uname = input('get.Uname');
		if($Smallchange){
			$sort = $Smallchange == 2?'desc':'asc';
			$order .=',Smallchange '.$sort;
		}
		if($AddTimeSort){
			$sort = $AddTimeSort == 2?'desc':'asc';
			$order .=',Addtime '.$sort;
		}
		if($LandTimeSort){
			$sort = $AddTimeSort == 2?'desc':'asc';
			$order .=',Land_time '.$sort;
		}
    	if($Uname){
    		$where['Username'] = ['like','%'.$Uname.'%'];
    	}
		if($StarAddTime || $EndAddTime){
            if(strtotime($EndAddTime)<strtotime($StarAddTime)){
                return '<script>alert("请选择正确的时间"),history.go(-1)</script>';
            }       
			$where['Addtime'] =array(['>=',strtotime($StarAddTime)],['<=',strtotime($EndAddTime)]);
        }
		if($StarLandTime || $EndLandTime){
            if(strtotime($EndLandTime)<strtotime($StarLandTime)){
                return '<script>alert("请选择正确的时间"),history.go(-1)</script>';
            }       
			$where['Land_time'] =array(['>=',strtotime($StarLandTime)],['<=',strtotime($EndLandTime)]);
        }
        if($State){
        	$where['State']=['=',$State];
        }
        if(!empty($this->REQUEST->only(['data']))){
            $file='会员数据统计';
            $name = array('昵称','总积分','平台积分','加入时间','是否关注公众号','账号状态','所在地' );
            $array = array('username','integral','data','addtime','scribe','status','address');
            excelExport($re,$file,$name,$array);
        }
		$result1 = model('User')->getUserCount($where,$order);
		//$this->assign('printingData',printingData($result1,'user'));
		$arr = [
			'Uname'=>$Uname,
			'State'=>$State,
			'Smallchange'=>$Smallchange,
			'AddTimeSort'=>$AddTimeSort,
			'LandTimeSort'=>$LandTimeSort,
			'StarAddTime'=>$StarAddTime,
			'EndAddTime'=>$EndAddTime,
			'StarLandTime'=>$StarLandTime,
			'EndLandTime'=>$EndLandTime,
		];
		$pageListr = model('User')->getUserPage($where,$order,$arr,12);
        $user_list = $pageListr->all(); 
		$arr['pageListr'] = $pageListr;
		$arr['user_list'] = $user_list;
		return view('index',$arr);
	}
	function card($id){
		$vm = model('User')->gerUserInfo($id);
		return view('card',['user_info'=>$vm]);
	}
	public function deleteUser($id)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		Db::startTrans();
        try{
            if($vm = Db::name('user')->where(array('id'=>$id))->find()){
				if(Db::name('user')->where(array('id'=>$id))->delete())
				Db::name('user_card')->where(array('User_id'=>$vm['User_id']))->delete();
			}
            Db::commit();
            return json(['code'=>1,'msg'=>'删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code'=>0,'msg'=>'删除失败']);
        }
	}
	public function toexamineUser($id,$sta)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		return model('User')->toexamineUser($id,$sta);
	}
	public function editUser($id,$nme,$sto)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		return model('User')->editUser($id,$nme,$sto);
	}
}
